// (C)2013 Reesoft
// GifAnimationTestDlg.cpp : implementation file

#include "stdafx.h"
#include "GifAnimationTest.h"
#include "GifAnimationTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CGifAnimationTestDlg dialog

CGifAnimationTestDlg::CGifAnimationTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGifAnimationTestDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGifAnimationTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK_STRETCH, m_bStretch);
	DDX_Control(pDX, IDC_GIF_ANIMATION_CTRL, m_GifAnimationCtrl);
}

BEGIN_MESSAGE_MAP(CGifAnimationTestDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN_IMAGE_FILE, &CGifAnimationTestDlg::OnBnClickedOpenImageFile)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_FROM_BUFFER, &CGifAnimationTestDlg::OnBnClickedLoadFromBuffer)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_FROM_RESOURCE, &CGifAnimationTestDlg::OnBnClickedLoadFromResource)
	ON_BN_CLICKED(IDC_BUTTON_START_ANIMATION, &CGifAnimationTestDlg::OnBnClickedStartAnimation)
	ON_BN_CLICKED(IDC_BUTTON_STOP_ANIMATION, &CGifAnimationTestDlg::OnBnClickedStopAnimation)
	ON_BN_CLICKED(IDC_CHECK_STRETCH, &CGifAnimationTestDlg::OnBnClickedStretch)
END_MESSAGE_MAP()

BOOL CGifAnimationTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGifAnimationTestDlg::OnOK()
{
}

void CGifAnimationTestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGifAnimationTestDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CGifAnimationTestDlg::OnBnClickedOpenImageFile()
{
	CFileDialog fdlg(TRUE);

	if (fdlg.DoModal() != IDOK)
	{
		return;
	}

	if (m_GifAnimationCtrl.LoadFromFile(fdlg.GetPathName()) != 0)
	{
		AfxMessageBox(_T("Failed to load image from file."), MB_ICONEXCLAMATION);
	}
}

void CGifAnimationTestDlg::OnBnClickedLoadFromBuffer()
{
	CFileDialog fdlg(TRUE);

	if (fdlg.DoModal() != IDOK)
	{
		return;
	}

	CFile File;
	CFileException FileException;

	if (!File.Open(fdlg.GetPathName(), CFile::modeRead, &FileException))
	{
		AfxMessageBox(_T("Failed to open file to read."), MB_ICONEXCLAMATION);
		return;
	}

	ULONG iSize = (ULONG) File.GetLength();

	if (iSize == 0)
	{
		AfxMessageBox(_T("File is empty."), MB_ICONEXCLAMATION);
		return;
	}

	BYTE* pBuffer = new BYTE[iSize];

	if (File.Read(pBuffer, iSize) != iSize)
	{
		AfxMessageBox(_T("Failed to read file."), MB_ICONEXCLAMATION);
		delete[] pBuffer;
		return;
	}

	if (m_GifAnimationCtrl.LoadFromBuffer(pBuffer, iSize) != 0)
	{
		AfxMessageBox(_T("Failed to load image from buffer."), MB_ICONEXCLAMATION);
	}

	delete[] pBuffer;
}

void CGifAnimationTestDlg::OnBnClickedLoadFromResource()
{
	if (m_GifAnimationCtrl.LoadFromResource((LONG) AfxGetInstanceHandle(), IDR_GIF, _T("GIF")) != 0)
	{
		AfxMessageBox(_T("Failed to load image from resource."), MB_ICONEXCLAMATION);
	}
}

void CGifAnimationTestDlg::OnBnClickedStartAnimation()
{
	m_GifAnimationCtrl.StartAnimation();
}

void CGifAnimationTestDlg::OnBnClickedStopAnimation()
{
	m_GifAnimationCtrl.StopAnimation();
}

void CGifAnimationTestDlg::OnBnClickedStretch()
{
	UpdateData();

	m_GifAnimationCtrl.SetStretch(m_bStretch);
}
